package com.zzld.modules.autoinn.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.session.HttpServletSession;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.abel533.entity.Example;
import com.zzld.core.common.config.GlobalStatic;
import com.zzld.core.utils.MD5Util;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.entity.Facilitator;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.autoinn.service.IFacilitatorService;

@Controller
@RequestMapping("/autoinn/wxfacilitator")
public class WXFacilitatorController {

	@Autowired
	private IFacilitatorService facilitatorService;

	@Autowired
	private ICustomerService customerService;

	private String facilitatorlogin = "/modules/autoinn/facilitator/login";
	private String facilitatorindex = "/modules/autoinn/facilitator/index";

	@RequestMapping(method = RequestMethod.GET)
	public String login(HttpServletRequest request) {
		Facilitator facilitator = null;
		try {
			facilitator = (Facilitator) request.getSession().getAttribute("fa");
			if (facilitator != null) {
				return facilitatorindex;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return facilitatorlogin;
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String a(HttpServletRequest request) {
		Facilitator facilitator = null;
		try {
			facilitator = (Facilitator) request.getSession().getAttribute("fa");
			if (facilitator != null) {
				return facilitatorindex;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return facilitatorlogin;
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String loginpost(Model model, HttpServletRequest request,
			HttpSession session) {
		Facilitator entity = new Facilitator();
		Customer customer = null;
		try {
			
			Example e = new Example(Customer.class);
			e.createCriteria().andEqualTo("phone_num",
					request.getParameter("account"));
			customer = customerService.selectByExample(e).get(0);
			if (customer == null) {
				model.addAttribute("message", "此用户不存在");
				return facilitatorlogin;
			}
			String password = request.getParameter("passwd");
			if (password!=null&& !password.equals("")) {
				password = MD5Util.encoderByMd5With32Bit(password);
			}

			entity.setWx_openid(customer.getOpenid());
			entity.setPassword(password);

			String code = (String) session
					.getAttribute(GlobalStatic.DEFAULT_CAPTCHA_PARAM);
			if (StringUtils.isNotBlank(code)) {
				code = code.toLowerCase().toString();
			}
			String submitCode = WebUtils.getCleanParam(request,
					GlobalStatic.DEFAULT_CAPTCHA_PARAM);
			if (StringUtils.isNotBlank(submitCode)) {
				submitCode = submitCode.toLowerCase().toString();
			}
			if (StringUtils.isBlank(submitCode) || StringUtils.isBlank(code)
					|| !code.equals(submitCode)) {
				model.addAttribute("message", "验证码错误!");
				return facilitatorlogin;
			}
			
			Example e2 = new Example(Facilitator.class);
			e2.createCriteria().andEqualTo("wx_openid", entity.getWx_openid())
			.andEqualTo("password", entity.getPassword());
			if(facilitatorService.selectByExample(e2).size()==0){
			   model.addAttribute("message", "用户不存在,请确认账号和密码!");
			   return facilitatorlogin;
			}
			entity.setId(facilitatorService.selectByExample(e2).get(0).getId());
			entity.setName(facilitatorService.selectByExample(e2).get(0).getName());
			entity.setType(facilitatorService.selectByExample(e2).get(0).getType());
			entity.setStoreid(facilitatorService.selectByExample(e2).get(0).getStoreid());
			entity.setPassword("");
			
			if(facilitatorService.selectByExample(e2).get(0).getState()==0){
				model.addAttribute("message", "此用户未启用!");
				return facilitatorlogin;
			}
			request.getSession().setAttribute("fa", entity);
		} catch (Exception e) {
			model.addAttribute("message", "未知错误,请链接管理员!");
			return facilitatorlogin;
		}
		return facilitatorindex;
	}

	@RequestMapping("index")
	public String index(Model model, HttpServletRequest request) {
		Facilitator name = (Facilitator) request.getSession().getAttribute("fa");
		if(name==null){
		   return	facilitatorlogin;
		}
		return facilitatorindex;
	}
	
	@RequestMapping("/loginout")
	public String loginout(HttpServletRequest request){
		Facilitator fa = (Facilitator)request.getSession().getAttribute("fa");
		if(fa!=null){
			 request.getSession().setAttribute("fa", null);
		}
		return facilitatorlogin;
	}

}
